package cn.itcast.nems.studentrecord.converter;

import cn.itcast.nems.common.utils.BigDecimalUtil;
import cn.itcast.nems.journal.dto.JournalRenewalDTO;
import cn.itcast.nems.journal.dto.JournalRenewalDiscountDTO;
import cn.itcast.nems.journal.dto.JournalRenewalLineDTO;
import cn.itcast.nems.journal.dto.JournalRenewalLineDiscountDTO;
import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordChangeDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordChangeDiscountDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liyong
 * create: 2023-11-20 15:07:43
 **/
public class StudentRecordConverter {

    public static StudentRecordChangeDTO renewalDTO(JournalRenewalDTO journalRenewalDTO,
                                                    JournalRenewalLineDTO journalRenewalLineDTO) {
        StudentRecordChangeDTO result = new StudentRecordChangeDTO();
        result.setJournalId(journalRenewalLineDTO.getJournalRenewalId());
        result.setJournalType(ReferenceTypeEnum.RENEWAL);
        result.setJournalLineId(journalRenewalLineDTO.getId());
        result.setStudentId(journalRenewalLineDTO.getStudentId());
        result.setOriginStudentRecordId(journalRenewalLineDTO.getOriginStudentRecordId());
        result.setOriginClazzId(journalRenewalLineDTO.getOriginClazzId());
        result.setOriginClazzName(journalRenewalLineDTO.getOriginClazzName());
        result.setOriginClazzOutDate(journalRenewalLineDTO.getOriginClazzOutDate());
        result.setTargetClazzId(journalRenewalDTO.getTargetClazzId());
        result.setTargetClazzName(journalRenewalDTO.getTargetClazzName());
        int price = BigDecimalUtil.bigDecimalToCentInt(journalRenewalDTO.getTargetClazzPrice());
        result.setTargetClazzPrice(price);
        int amount = BigDecimalUtil.bigDecimalToCentInt(journalRenewalLineDTO.getTargetClazzAmountDue());
        result.setTargetClazzAmountDue(amount);
        amount = BigDecimalUtil.bigDecimalToCentInt(journalRenewalLineDTO.getTargetClazzAmountDue());
        result.setTargetClazzActualAmountDue(amount);
        amount = BigDecimalUtil.bigDecimalToCentInt(journalRenewalLineDTO.getTargetClazzDiscountAmount());
        result.setTargetClazzDiscountAmount(amount);
        result.setTargetClazzInDate(journalRenewalLineDTO.getTargetClazzInDate());
        final List<StudentRecordChangeDiscountDTO> discounts = getStudentRecordDiscounts(journalRenewalDTO, journalRenewalLineDTO);
        result.setDiscounts(discounts);
        result.setRemark(journalRenewalDTO.getRemark());
        return result;
    }

    private static List<StudentRecordChangeDiscountDTO> getStudentRecordDiscounts(JournalRenewalDTO journalRenewalDTO,
                                                                                  JournalRenewalLineDTO journalRenewalLineDTO) {
        List<StudentRecordChangeDiscountDTO> discounts = new ArrayList<>();
        final List<JournalRenewalDiscountDTO> discountList = journalRenewalDTO.getDiscountList();
        if (discountList != null && !discountList.isEmpty()) {
            discountList.forEach(d -> {
                StudentRecordChangeDiscountDTO discount = new StudentRecordChangeDiscountDTO();
                discount.setDiscountId(d.getDiscountId());
                discount.setDiscountAmount(BigDecimalUtil.bigDecimalToCentInt(d.getDiscountAmount()));
                discount.setSort(d.getSort().byteValue());
                discounts.add(discount);
            });
        }
        final List<JournalRenewalLineDiscountDTO> discountList1 = journalRenewalLineDTO.getDiscountList();
        if (discountList1 != null && !discountList1.isEmpty()) {
            discountList1.forEach(d -> {
                StudentRecordChangeDiscountDTO discount = new StudentRecordChangeDiscountDTO();
                discount.setDiscountId(d.getDiscountId());
                discount.setDiscountAmount(BigDecimalUtil.bigDecimalToCentInt(d.getDiscountAmount()));
                discount.setSort(d.getSort().byteValue());
                discounts.add(discount);
            });
        }
        List<JournalRenewalLineDiscountDTO> discountCounponList =  Objects.requireNonNullElse(journalRenewalLineDTO.getDiscountCouponList(),new ArrayList<>()) ;
        for (JournalRenewalLineDiscountDTO journalRenewalLineDiscountDTO : discountCounponList) {
            StudentRecordChangeDiscountDTO discount = new StudentRecordChangeDiscountDTO();
            discount.setDiscountId(journalRenewalLineDiscountDTO.getDiscountId());
            discount.setDiscountAmount(BigDecimalUtil.bigDecimalToCentInt(journalRenewalLineDiscountDTO.getDiscountAmount()));
            discount.setSort(journalRenewalLineDiscountDTO.getSort().byteValue());
            discounts.add(discount);
        }
        return discounts;
    }
}
